package com.fenqing.leetcode.code.t0_200.t100_200;

import com.fenqing.annotations.Execute;
import com.fenqing.bean.Node;
import com.fenqing.core.Topic;
import com.fenqing.utils.Node1ToStringEncoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Topic133 extends Topic {
    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic133(String input) {
        super(input);
    }

    @Execute
    public Node cloneGraph(Node node) {
        if (node == null) {
            return null;
        }
        return cloneGraph(node, new HashMap<>());
    }

    public Node cloneGraph(Node node, Map<Integer, Node> node1Map) {
        Node node1 = node1Map.get(node.val);
        if (node1 != null) {
            return node1;
        }
        node1 = new Node(node.val);
        node1Map.put(node.val, node1);
        node1.neighbors = new ArrayList<>();
        for (Node neighbor : node.neighbors) {
            Node node11 = cloneGraph(neighbor, node1Map);
            node1.neighbors.add(node11);
        }
        return node1;
    }

    public static void main(String[] args) {
        Topic133 topic133 = new Topic133("""
                [[2,4],[1,3],[2,4],[1,3]]
                """);
        Node run = topic133.run();
        System.out.println(new Node1ToStringEncoder().encoder(run));
    }
}
